#include "pch.h"
#include <iostream>
#include <conio.h>
#include "book.h"
#include "binarySerialize.hpp"

vector<book> bookManage::bookList;
vector<reader> bookManage::readerList;
list<string> bookManage::scrapRecord;
Date bookManage::today;

reader* userManager::loginUser=nullptr;
bool userManager::stat;
readerType userManager::stRt;
readerType userManager::teRt;

string path;

char mainChoose;
char menuChoose;

void opFinish(bool i=true)
{
	if (i)
		_getch();
	mainChoose = 0;
	menuChoose = 0;
}

void title()
{
	cout << "*********图书管理系统**********" << endl;
	cout << "*******今天是" << bookManage::today.toString() << "*******" << endl;
}

bool mainMenu()
{
	title();
	if (userManager::stat)
	{
		cout << "a.借阅资料管理" << endl;
		if (mainChoose == 'a')
		{
			cout << "    a.存书" << endl;
			cout << "    b.报废" << endl;
			cout << "    c.修改" << endl;
			cout << "    d.查询" << endl;
			cout << "    e.报废列表" << endl;
			cout << "    0.回退" << endl;

			string name, author, press;
			Date d;
			int money;
			string type;
			int number;

			if (menuChoose == 'a')
			{
				cout << "请输入要增加的书籍信息:书名，作者，出版社。" << endl;
				cin >> name >> author >> press;
				cout << "请输入出版日期年，月，日。" << endl;
				cin >> d.year >> d.month >> d.day;
				cout << "请输入金额，类别，库存。" << endl;
				cin >> money >> type >> number;
				bookManage::addBook(name, author, press, d, money, type, number);
				cout << "存书成功！" << endl;

				opFinish();
				return true;
			}
			if (menuChoose == 'b')
			{
				cout << "请输入你要报废的书名：" << endl;
				cin >> name;
				bookManage::removeBook(name);
				cout << "报废成功！" << endl;

				opFinish();
				return true;
			}
			if (menuChoose == 'c')
			{
				cout << "请输入你要修改的书的书名";
				cin >> name;
				book* b = bookManage::findBook(name);
				cout << "请输入要修改的金额：" << endl;
				cin >> b->money;
				cout << "修改成功！" << endl;

				opFinish();
				return true;
			}
			if (menuChoose == 'd')
			{
				cout << "请输入你要查询的书的书名";
				cin >> name;
				cout << "信息如下" << endl;
				book* b = bookManage::findBook(name);
				b->display();

				opFinish();
				return true;
			}
			if (menuChoose == 'f')
			{
				for (string i : bookManage::scrapRecord)
					cout << i << endl;
				opFinish();
				return true;
			}
		}
	}
	cout << "b.借阅管理" << endl;
	if (mainChoose == 'b')
	{
		cout << "    a.借出操作" << endl;
		cout << "    b.还书操作" << endl;
		cout << "    c.续借处理" << endl;
		cout << "    0.回退" << endl;

		string bookname, readername;

		if (menuChoose == 'a')
		{
			cout << "请输入要借阅的书名：" << endl;
			cin >> bookname;
			book* b = bookManage::findBook(bookname);
			reader* r = bookManage::findreader(readername);
			if (b == nullptr)
			{
				cout << "找不到这本书" << endl;
			}
			else if (r == nullptr)
			{
				cout << "找不到这个读者" << endl;
			}
			else
			{
				if(r->borrow(b, bookManage::today))
					cout << "借阅成功！" << endl;
				else
					cout << "借阅失败！" << endl;
			}
			opFinish();
			return true;
		}
		if (menuChoose == 'b')
		{
			cout << "请输入要还的书名：" << endl;
			cin >> bookname;
			book* b = bookManage::findBook(bookname);
			reader* r = bookManage::findreader(readername);
			if (b == nullptr)
			{
				cout << "找不到这本书" << endl;
			}
			else if (r == nullptr)
			{
				cout << "找不到这个读者" << endl;
			}
			else
			{
				if(r->ret(b, bookManage::today))
					cout << "还书成功！" << endl;
				else
					cout << "还书失败！" << endl;
			}
			opFinish();
			return true;
		}
		if (menuChoose == 'c')
		{
			cout << "请输入要续借的书名：" << endl;
			cin >> bookname;
			book* b = bookManage::findBook(bookname);
			if (b == nullptr)
			{
				cout << "找不到这本书" << endl;
			}
			else
			{
				reader* r = bookManage::findreader(userManager::loginUser->name);
				if(r->con(b))
					cout << "续借成功！" << endl;
				else
					cout << "续借失败！" << endl;
			}
			opFinish();
			return true;
		}
	}
	if (userManager::stat)
	{
		cout << "c.读者管理" << endl;
		if (mainChoose == 'c')
		{
			cout << "    a.添加读者" << endl;
			cout << "    b.删除读者" << endl;
			cout << "    c.读者挂失处理" << endl;
			cout << "    d.查询读者" << endl;
			cout << "    0.回退" << endl;

			int no;
			string name, c, type, key;


			if (menuChoose == 'a')
			{
				cout << "请输入读者类型" << endl;
				cin >> type;
				cout << "请输入你要添加的读者的学号，姓名，班级，密码" << endl;
				cin >> no >> name >> c >> key;

				if (type == "学生")
					bookManage::addreader(no, name, c, key, userManager::stRt);
				else
					bookManage::addreader(no, name, c, key, userManager::teRt);

				cout << "添加成功！" << endl;

				opFinish();
				return true;
			}
			if (menuChoose == 'b')
			{
				cout << "请输入你要删除的读者姓名" << endl;
				cin >> name;
				bookManage::removereader(name);
				cout << "删除成功！" << endl;

				opFinish();
				return true;
			}
			if (menuChoose == 'c')
			{
				cout << "请输入你要挂失的姓名" << endl;
				cin >> name;
				reader* r = bookManage::findreader(name);
				if (r == nullptr)
				{
					cout << "读者库没有此人信息" << endl;
				}
				else
				{
					if (r->isLose == true)
					{
						cout << "已经挂失" << endl;
						cout << "是否更改挂失情况(Y/N)" << endl;
						menuChoose = _getch();
						if (menuChoose == 'Y')
						{
							cout << "挂失已取消" << endl;
						}

					}
					else if (r->isLose == false)
					{
						cout << "挂失成功" << endl;
					}
				}
				opFinish();
				return true;
			}
			if (menuChoose == 'd')
			{
				cout << "请输入你要查询的读者" << endl;
				cin >> name;
				cout << "信息如下" << endl;
				reader* r = bookManage::findreader(name);
				if (r == nullptr)
					cout << "读者库没有此人信息" << endl;
				else
					r->display(bookManage::today);

				opFinish();
				return true;
			}
		}
		cout << "d.统计分析" << endl;
		if (mainChoose == 'd')
		{
			cout << "    a.持书数量排行" << endl;
			cout << "    b.借书总数排行" << endl;
			cout << "    c.当前图书借阅量排行" << endl;
			cout << "    d.图书总借阅量排行" << endl;
			cout << "    e.超期未还图书排行" << endl;
			cout << "    0.回退" << endl;

			if (menuChoose != 0)
			{
				if (menuChoose == 'a')
					statistics::readerRank1();
				else if (menuChoose == 'b')
					statistics::readerRank2();
				else if (menuChoose == 'c')
					statistics::bookRank1();
				else if (menuChoose == 'd')
					statistics::bookRank3();
				else if (menuChoose == 'e')
					statistics::bookRank2();
				opFinish();
				return true;
			}
		}
		cout << "e.硬盘存储与读取" << endl;
		if (mainChoose == 'e')
		{
			cout << "    a.图书硬盘存储" << endl;
			cout << "    b.图书硬盘读取" << endl;
			cout << "    c.读者硬盘存储" << endl;
			cout << "    d.读者硬盘读取" << endl;
			cout << "    0.回退" << endl;

			if (menuChoose != 0)
			{
				if (menuChoose == 'a')
				{
					binarySerialize::saveSTLObject<vector<book>, book>(path, bookManage::bookList);
					cout << "存储成功！" << endl;
				}
				if (menuChoose == 'b')
				{
					binarySerialize::loadSTLObject<vector<book>, book>(path, bookManage::bookList);
					cout << "读取成功！" << endl;
				}
				if (menuChoose == 'c')
				{
					binarySerialize::saveSTLObject<vector<reader>, reader>(path + "2", bookManage::readerList);
					cout << "存储成功！" << endl;
				}
				if (menuChoose == 'd')
				{
					binarySerialize::loadSTLObject<vector<reader>, reader>(path + "2", bookManage::readerList);
					cout << "读取成功！" << endl;
				}
				opFinish();
				return true;
			}
		}
		cout << "f.更改系统时间" << endl;
		if (mainChoose == 'f')
		{
			cout << "请输入年，月，日" << endl;
			cin >> bookManage::today.year >> bookManage::today.month >> bookManage::today.day;
			cout << "更新完成！" << endl;
			opFinish();
			return true;
		}
	}
	cout << "g.退出登陆" << endl;
	if (mainChoose == 'g')
	{
		userManager::loginUser = nullptr;
		opFinish(false);
		return true;
	}
	return false;
}

int main()
{
	path = "D:/save";
	opFinish(false);
	userManager::stRt = readerType("学生", 1, 3);
	userManager::teRt = readerType("老师", 2, 6);
	bookManage::addreader(000, "初始管理员", "", "123456", userManager::teRt);
	bookManage::addreader(001, "初始用户", "", "123456", userManager::stRt);

	while (1)
	{
		title();
		cout << "请输入用户姓名：" << endl;
		string name;
		cin >> name;
		cout << "请输入用户密码：" << endl;
		string key;
		cin >> key;
		if (userManager::login(name, key))
		{
			cls();
			while (1)
			{
				if (mainMenu() == false)
				{
					if (userManager::loginUser == nullptr) //退出登陆
						break;

					if (mainChoose == 0)
						mainChoose = _getch();
					else if (menuChoose == 0)
						menuChoose = _getch();

					if (menuChoose == '0') //回退
						opFinish(false);
				}
				cls();
			}
		}
		else
		{
			cout << "登陆失败！" << endl;
			_getch();
		}
		cls();
	}
}
